#include <bits/stdc++.h>

#define N 200050
#define inf 0x3f3f3f3f

using namespace std;

int T;

int n, ans = -inf;
int a[N], sum[N], h[1000050];

void dfs(int x, int s, int lr, int lb){
	if(x == n) {
		ans = max(ans, s);
		return ;
	}
	if(s + sum[x + 1] <= ans) return ;
	
	int res = s;
	if(a[lr] == a[x + 1]) res = s + a[x + 1];
	dfs(x + 1, res, x + 1, lb);
	
	res = s;
	if(a[lb] == a[x + 1]) res = s + a[x + 1];
	dfs(x + 1, res, lr, x + 1);
}

int main(){
	freopen("color.in", "r", stdin);
	freopen("color.out", "w", stdout);

	cin >> T;
	while(T--){
		memset(h, 0, sizeof h);
		memset(sum, 0, sizeof sum);
		ans = -inf;
		
		int f = 0;
		cin >> n;
		for(int i = 1; i <= n; i++){
			cin >> a[i];
			if(h[a[i]]) f = 1;
			h[a[i]]++;
		}
		
		if(!f){
			cout << 0 << "\n";
			continue;
		}
		
		for(int i = n; i >= 1; i--){
			if(h[a[i]] > 1) sum[i] = a[i] + sum[i + 1];
			else sum[i] = sum[i + 1];
			h[a[i]]--;
		} 
			
		dfs(0, 0, 0, 0);
		
		cout << ans << "\n";
	}
	return 0;
}
